package com.ruoyi.quartz.task;

import com.apistd.uni.Uni;
import com.apistd.uni.UniException;
import com.apistd.uni.UniResponse;
import com.apistd.uni.sms.UniMessage;
import com.apistd.uni.sms.UniSMS;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.slope.warning.info.domain.SlopeWarningInfo;
import com.ruoyi.system.service.impl.SysDeptServiceImpl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MessageSending {
    public static String ACCESS_KEY_ID = "mecMjtBsKsE69Zt431oMfTAF97Ahnrcfrmx7gYx6GKqmsVwtF";
    static SysDeptServiceImpl sysDeptService = new SysDeptServiceImpl();


    public static void send(SlopeWarningInfo slopeWarningInfo) throws UniException {
        // 初始化
        Uni.init(ACCESS_KEY_ID); // 若使用简易验签模式仅传入第一个参数即可

        // 获取dept_name
        SysDept sysDept = new SysDept();
        sysDept.setDeptId(slopeWarningInfo.getDeptId());
        List<SysDept> sysDeptList = sysDeptService.selectDeptList(sysDept);
        String deptName = sysDeptList.get(0).getDeptName();
        // 获取负责人user_name
        String userName = sysDeptList.get(0).getLeader();
        // 获取负责人phone
        String phone = sysDeptList.get(0).getPhone();
        // 获取边坡名称
        String slopeName = slopeWarningInfo.getSlopeName();
        // 获取监测类型
        String monitorType = slopeWarningInfo.getMonitorType();
        // 获取监测点id
        String monitorPoint = slopeWarningInfo.getPointNumber();
        // 获取时间
        Date time = slopeWarningInfo.getCreateTime();
        // 获取预警等级
        String warningLevel = slopeWarningInfo.getWarningLevel();
        // 获取预警信息
        String warningType = slopeWarningInfo.getWarningType();
        // 开始时间
        Date startTime = slopeWarningInfo.getStartTime();
        // 结束时间
        Date endTime = slopeWarningInfo.getEndTime();

        // 设置自定义参数 (变量短信)
        Map<String, String> templateData = new HashMap<String, String>();
        templateData.put("dept_name", deptName);
        templateData.put("user_name", userName);
        templateData.put("slope_name", slopeName);
        templateData.put("monitor_type", monitorType);
        templateData.put("monitor_point", monitorPoint);
        templateData.put("time", String.valueOf(time));
        templateData.put("warning_level", warningLevel);
        templateData.put("start_time", String.valueOf(startTime));
        templateData.put("end_time", String.valueOf(endTime));
        templateData.put("warning_info", warningType);

        // 构建信息
        UniMessage message = UniSMS.buildMessage()
                .setTo(phone)
                .setSignature("UniSMS")
                .setTemplateId("slope_warning")
                .setTemplateData(templateData);

        // 发送短信
        try {
            UniResponse res = message.send();
            System.out.println(res);
        } catch (UniException e) {
            System.out.println("Error: " + e);
            System.out.println("RequestId: " + e.requestId);
        }
    }
}
